package snowforce;

import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;

import javax.ws.rs.GET;
import javax.ws.rs.Path;
import javax.ws.rs.Produces;
import javax.ws.rs.QueryParam;
import javax.ws.rs.core.MediaType;

import snowforce.datatype.HintJSON;
import snowforce.datatype.PassPoint;
import snowforce.datatype.RouteEdgeJSON;
import snowforce.datatype.RouteJSON;
import snowforce.storage.HintJSONStore;
import snowforce.storage.HintStore;
import snowforce.storage.PassPointStore;

@Path("/hint")
public class HintService
{
	@GET
	@Produces(MediaType.APPLICATION_JSON)
	public HintJSON getHintJSON(@QueryParam("level") String levelStr, @QueryParam("beginIndex") String beginIndexStr, @QueryParam("endIndex") String endIndexStr, @QueryParam("route") String routesStr)
	{
		HintJSON ret = new HintJSON();
		// ret = getDefault();
		int level, beginIndex, endIndex;
		try {
			level = Integer.parseInt(levelStr);
		}
		catch (Exception e) {
			e.printStackTrace();
			ret.setStatus("PARAM_LEVEL_WRONG");
			return ret;
		}
		try {
			beginIndex = Integer.parseInt(beginIndexStr);
		}
		catch (Exception e) {
			e.printStackTrace();
			ret.setStatus("PARAM_BEGININDEX_WRONG");
			return ret;
		}
		try {
			endIndex = Integer.parseInt(endIndexStr);
		}
		catch (Exception e) {
			e.printStackTrace();
			ret.setStatus("PARAM_ENDINDEX_WRONG");
			return ret;
		}
		if (beginIndex > endIndex) {
			System.out.println("beginIndex>endIndex");
			ret.setStatus("beginIndex>endIndex");
			return ret;
		}
		// ret.getRoutes().add(new Route());
		// loadPassPointFile(level + "");
		// loadHintFile(level + "");
		// generateHint(level);
		System.out.println("level:" + level);
		List<RouteJSON> routes = getRoutes(level, beginIndex, endIndex, routesStr);
		ret = new HintJSON("OK", routes);
		return ret;
	}

	private List<RouteEdgeJSON> getHintJSONList(int level, int beginIndex, int endIndex, String routesStr)
	{
		// TODO Auto-generated method stub
		List<PassPoint> ppl = PassPointStore.getStore().get("" + level);
		List<RouteEdgeJSON> rejl = HintStore.getStore().get("" + level);
		String beginPoint = ppl.get(beginIndex).getPoint() + "";
		String endPoint = ppl.get(endIndex).getPoint() + "";
		if (beginPoint.length() < 2) {
			beginPoint = "0" + beginPoint;
		}
		if (endPoint.length() < 2) {
			endPoint = "0" + endPoint;
		}
		int index1 = rejl.size() - 1, index2 = 0;
		int i = 0;
		for (Iterator<RouteEdgeJSON> iterator = rejl.iterator(); iterator.hasNext();) {
			RouteEdgeJSON rej = (RouteEdgeJSON) iterator.next();
			String id = rej.getId();
			if ((id.indexOf(beginPoint) == 0 || id.indexOf(beginPoint) == 2) && index1 > i) {
				index1 = i;
			}
			if ((id.indexOf(endPoint) == 0 || id.indexOf(endPoint) == 2) && index2 < i) {
				index2 = i;
			}
			i++;
		}

		return rejl.subList(index1, index2);

	}

	private List<RouteJSON> getRoutes(int level, int beginIndex, int endIndex, String routesStr)
	{
		ArrayList<RouteJSON> routes = new ArrayList<RouteJSON>();
		// TODO FIXME
		List<List<RouteEdgeJSON>> rejl = HintJSONStore.getStore().get("" + level);
		// List<List<RouteEdgeJSON>> rejl = hjs.get("" + level);
		int index = beginIndex;
		while (index < endIndex) {
			routes.add(new RouteJSON(rejl.get(index)));
			index++;
		}
		return routes;
	}

	private HintJSON getDefault()
	{
		HintJSON h = new HintJSON();

		h.setStatus("OK");
		List<RouteJSON> routes = new ArrayList<RouteJSON>();
		RouteJSON route = new RouteJSON();
		List<RouteEdgeJSON> edgeList = new ArrayList<RouteEdgeJSON>();
		RouteEdgeJSON e = new RouteEdgeJSON("01");
		edgeList.add(e);
		e = new RouteEdgeJSON("02");
		edgeList.add(e);
		e = new RouteEdgeJSON("03");
		edgeList.add(e);
		route.setRoute(edgeList);
		routes.add(route);

		route = new RouteJSON();
		edgeList = new ArrayList<RouteEdgeJSON>();
		e = new RouteEdgeJSON("012");
		edgeList.add(e);
		e = new RouteEdgeJSON("023");
		edgeList.add(e);
		e = new RouteEdgeJSON("034");
		edgeList.add(e);
		route.setRoute(edgeList);
		routes.add(route);

		h.setRoutes(routes);
		System.out.println(h);
		return h;
	}

}